<extend name="Public/base"/>

<block name="style">
    <style>
      table td {
        text-align: center;
      }
    </style>
</block>

<block name="body">
    <script type="text/javascript" src="__STATIC__/uploadify/jquery.uploadify.min.js"></script>
    <div class="main-title">
        <h2>{$meta_title}</h2>
    </div>

    <form action="" method="post" class="form-horizontal">

        <!--  书籍名称  -->
        <div class="form-item">
            <label class="item-label">书籍名称</label>
            <div class="controls">
                <input type="text" class="text input-large" name="name" value="{$info.name}">
            </div>
        </div>
        <!--  出版社  -->
        <div class="form-item">
            <label class="item-label">出版社</label>
            <div class="controls">
                <input type="text" class="text input-large" name="press" value="{$info.press}">
            </div>
        </div>
        <!--  作者  -->
        <div class="form-item">
            <label class="item-label">作者</label>
            <div class="controls">
                <input type="text" class="text input-large" name="author" value="{$info.author}">
            </div>
        </div>
        <!--  出版时间  -->
        <div class="form-item">
            <label class="item-label">出版时间</label>
            <div class="controls">
                <input type="text" class="text input-large" name="publication_time" value="{$info.publication_time}">
            </div>
        </div>
        <!--  商品分类  -->
        <div class="form-item">
            <label class="item-label">商品分类</label>
            <div class="controls">
                <select name="one_type_id" class="one_type">
                    <option value="0">请选择一级分类</option>
                </select>
                <select name="two_type_id" class="two_type">
                    <option value="0">请选择二级分类</option>
                </select>
            </div>
        </div>
        <!--  规格  -->
        <div class="form-item">
            <label class="item-label">规格</label>
            <div class="controls">
                <div class="data-table table-striped" style="width: 900px">
                    <table border="1">
                        <tr>
                            <th>规格</th>
                            <th>封面图</th>
                            <th>编码</th>
                            <th>库存</th>
                            <th>成本价</th>
                            <th>销售价</th>
                        </tr>
                        <volist name="spec_list" id="vo">
                            <tr>
                                <td>{$vo.spec}</td>
                                <td><img src="{$vo.img}" width="50px" height="50px" object-fit="cover"/></td>
                                <td>{$vo.code}</td>
                                <td>{$vo.count}</td>
                                <td>{$vo.old_price}</td>
                                <td>
                                    <input type="number" class="edit_price" value="{$vo.price}" name="price" style="height: 40px" onblur="changePrice(this, '{$vo.id}', '{$vo.old_price}')">
                                </td>
                            </tr>
                        </volist>
                    </table>
                </div>
            </div>
        </div>

        <!--  是否推荐  -->
        <div class="form-item">
            <label class="item-label">是否推荐</label>
            <div class="controls">
                <label><input type="radio" name="is_recommend" value="0" <if condition="$info['is_recommend'] == 0">checked</if>> 否</label>
                <label><input type="radio" name="is_recommend" value="1" <if condition="$info['is_recommend'] == 1">checked</if>> 是</label>
            </div>
        </div>

        <!--  书籍详情  -->
        <div class="form-item">
            <label class="item-label">书籍详情</label>
            <div class="controls">
                <label class="textarea input-large" style="width: 1250px;">
                    <textarea name="detail">{$info.detail}</textarea>
                    {:hook("adminArticleEdit", array("name"=>detail,"value"=>$info["detail"]))}
                </label>
            </div>
        </div>

        <input type="hidden" value="{:I('id')}" name="id"/>
        <input type="hidden" class="spec_list" name="spec_list"/>
        <div class="form-item">
            <button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">确 定</button>
            <button class="btn btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
        </div>
    </form>
</block>

<block name="script">
    <script type="text/javascript">
        var goods_type_list = {$goods_type_list_json};
        var one_type_id = "{$info.one_type_id}";
        var two_type_id = "{$info.two_type_id}";
        if (goods_type_list.length > 0) {
            var one_option_str = "<option value='0'>请选择一级分类</option>";
            var two_option_str = "<option value='0'>请选择二级分类</option>";
            goods_type_list.forEach(function (val) {

                if (val.id == one_type_id) {
                    var one_selected = 'selected';

                    var two_type_list = val.son_list ? val.son_list : [];
                    if (two_type_list.length > 0) {
                        two_type_list.forEach(function (value) {
                            if (value.id == two_type_id) {
                                var two_selected = 'selected';
                            }

                            two_option_str += "<option value='" + value.id + "' " + two_selected + ">" + value.name + "</option>";
                        })
                    }
                }
                one_option_str += "<option value='" + val.id + "' " + one_selected + ">" + val.name + "</option>";
            })

            $('.one_type').html(one_option_str);
            $('.two_type').html(two_option_str);
        }

        $('.one_type').change(function () {
            var change_one_type_id = $(this).val();

            var two_option_str = "<option value='0'>请选择二级分类</option>";
            if (goods_type_list.length > 0) {
                goods_type_list.forEach(function (val) {
                    if (val.id == change_one_type_id) {
                        var two_type_list = val.son_list ? val.son_list : [];
                        if (two_type_list.length > 0) {
                            two_type_list.forEach(function (value) {
                                two_option_str += "<option value='" + value.id + "'>" + value.name + "</option>";
                            })
                        }
                    }
                })
                $('.two_type').html(two_option_str);
            }
        })

        var spec_list = [];
        function changePrice(that, id, old_price) {
            var price = $(that).val();

            if (parseFloat(old_price) > parseFloat(price) || price == '') {
                var index = $('.edit_price').index(that);

                var edit_old_price;
                var have = false;
                if (spec_list.length > 0) {
                    for (const key in spec_list) {
                        if (spec_list[key].id == id) {
                            edit_old_price = spec_list[key].price;
                            have = true;
                            break;
                        }
                    }
                }
                if(have){
                    $('.edit_price').eq(index).val(edit_old_price);
                }else{
                    $('.edit_price').eq(index).val(old_price);
                }

                layer.msg('销售价不能小于成本价');
            }else{
                var single_spec = {};

                single_spec.id = id;
                single_spec.price = price;

                var is_new = false;
                if (spec_list.length > 0) {
                    for (const key in spec_list) {
                        if (spec_list[key].id == id) {
                            spec_list[key] = single_spec;
                            is_new = true;
                            break;
                        }
                    }

                }
                if (!is_new) spec_list.push(single_spec);

                $('.spec_list').val(JSON.stringify(spec_list));
            }
        }


        //导航高亮
        highlight_subnav('{:U("Manage/goodsList")}');
    </script>
</block>